package com.example.menuvirtualclient;


import com.example.menuvirtualclient.adapter.TableAdapter;
import com.example.menuvirtualclient.core.*;
import com.example.menuvirtualclient.presenter.*;
import com.example.menuvirtualclient.util.Common;
import com.example.menuvirtualclient.view.ITableView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SettingsActivity extends Activity implements ITableView {
	String EXTRA_MESSAGE = "com.example.menuvirtualclient.settingactivity";

	private TablePresenter _presenter;	
	private Table _table;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		if(savedInstanceState == null)
			Log.e("Error - onCreate() class SettingsActivity", "savedInstanceState is null.");
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		
		_presenter = new TablePresenter(this);
		_presenter.LoadList();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_settings, menu);
		return true;
	}

	@Override
	public void SetMessage(String title, String message) {
		ShowToast(message);
	}

	@Override
	public Context GetContext() {
		return getApplicationContext();
	}
	
	public void updateClient(View view){
		Intent i = new Intent(this, FullscreenActivity.class);
		i.putExtra("com.example.menuvirtualclient.update", true);
		startActivity(i);
	}

	@Override
	public void ShowTables(String[] tablesName) {
		ListView listMesas = (ListView) findViewById(R.id.listMesas);
		ArrayAdapter<String> listArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tablesName);
		listMesas.setAdapter(listArrayAdapter);

		listMesas.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				//Intent myIntent = new Intent(getApplicationContext(), FullscreenActivity.class);
				//String mesa = ((TextView)arg1).getText().toString();
				//myIntent.putExtra(EXTRA_MESSAGE, mesa);
				//startActivity(myIntent);
				String table = ((TextView)arg1).getText().toString();
				String[] arrayValue = table.split("-");
//				_presenter.GetByName(arrayValue[0]);
				_presenter.GetById(Integer.valueOf((arrayValue[0].replace(" ", ""))));
			}
		});
	}

	private void ShowToast(String message) {
		Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT);
		toast.show();
	}
	
	@Override
	public void CloseView() {
		finish();
	}

	@Override
	public void ShowTables(Table[] tables) {
		ListView listMesas = (ListView) findViewById(R.id.listMesas);
		TableAdapter tableAdapter = new TableAdapter(this, R.layout.activity_settings, tables);
		listMesas.setAdapter(tableAdapter);
	}

	@Override
	public void SetTableSelected(Table table) {
		FullscreenActivity.table = table;
		//1) recupera  la orden desde el WS
		WsOrderClient orderClient = new WsOrderClient(getApplicationContext());
		orderClient.execute(Common.URL_WS + "getOrderByTable",
				Integer.toString(table.getId()), 
				Integer.toString(FullscreenActivity.waiter.getId()));
		/*
		String request = orderClient.getRequest();
		String[] arreglo = request.split(",");
		//2) Serializa el request para recuperar los datos faltantes, son los que estan comentados
		Order order = new Order();
		order.setId(Integer.parseInt(arreglo[0]));
		order.setStatus(Integer.parseInt(arreglo[1]));
		//order.setDate(date);
		order.setTable(table);
		order.setUser(FullscreenActivity.waiter);
		//3)descomenta el presenter para persistir la orden en el android.
		_presenter.SaveOrder(order);
		*/
		console(FullscreenActivity.waiter, table);
	}

	private void console(User waiter, Table table) {
		Log.i("Info", "Mesero:" + waiter.getUserName() + " - Mesa:" + table.getName());
	}
	
}
